package com.eastfair.projectcore.service.impl;

import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.auth.InterfaceMenuCacheKeyBuilder;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.projectcore.dao.InterfaceMenuMapper;
import com.eastfair.projectcore.entity.InterfaceInfo;
import com.eastfair.projectcore.entity.InterfaceMenu;
import com.eastfair.projectcore.service.InterfaceMenuService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import com.eastfair.uid.baidu.rule.SnowFlakeRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.eastfair.projectcore.vo.*;
import com.eastfair.projectcore.dto.*;

/**
 * <p>
 * 业务实现类
 * 接口与菜单关系
 * </p>
 *
 * @author ligang
 * @date 2023-05-11
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class InterfaceMenuServiceImpl extends SuperCacheServiceImpl<InterfaceMenuMapper, InterfaceMenu> implements InterfaceMenuService {
    @Autowired
    SnowFlakeRole snowFlakeRole;
    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(InterfaceMenu model) {
        model.setId(snowFlakeRole.findUidSimple());
        return R.successDef();
    }

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<InterfaceMenu> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<InterfaceMenu> list(InterfaceMenuDTO modelDTO, List<Long> ids) {
        return ((InterfaceMenuService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'menuId', #args[0]?.menuId}," +
                            "{'method', #args[0]?.method}," +
                            "{'fullUrl', #args[0]?.fullUrl}," +
                            "{'interfaceId', #args[0]?.interfaceId}," +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<InterfaceMenu> listOfCache(InterfaceMenuDTO modelDTO, List<Long> ids) {
        //查询
        InterfaceMenu queryModel = new InterfaceMenu();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO, queryModel);
        }

        QueryWrap<InterfaceMenu> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(InterfaceMenu::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(InterfaceMenu::getIsDeleted, BusinessConstant.DELETE_NO);
        List<InterfaceMenu> modelList = list(wrapper);
        return modelList;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public InterfaceMenuVO doToVo(InterfaceMenu model) {
        if (model == null) {
            return null;
        }
        InterfaceMenuVO modelVo = new InterfaceMenuVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<InterfaceMenuVO> doToVos(List<InterfaceMenu> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<InterfaceMenuVO> result = new ArrayList<>();
        for (InterfaceMenu model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }


    @Override
    public CacheKeyBuilder getCacheKeyBuilder() {
        return new InterfaceMenuCacheKeyBuilder();
    }
}
